//
//  WLHttpManager.swift
//
//  Created by Visitor on 2017/5/15.
//  Copyright © 2017年 Visitor. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType: String {
    case GET = "GET"
    case POST = "POST"
}

class WLHttpManager: AFHTTPSessionManager {
    static let sharHttpManager: WLHttpManager = {
        let tools = WLHttpManager()
        tools.requestSerializer.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
        tools.responseSerializer.acceptableContentTypes =  NSSet(objects: "application/json", "text/json", "text/javascript", "text/plain", "text/html") as? Set<String>
        //设置证书--解决 https 问题
        tools.securityPolicy.validatesDomainName = false
        tools.responseSerializer = AFHTTPResponseSerializer()
     
        return tools
    }()
    
    func request(methodType: RequestType, urlString: String ,params: [String: AnyObject], success:@escaping ( (_ result: Any) ->()), failed:@escaping ((_ error: Error?) ->()) ){
        let successCallBack = { (task: URLSessionDataTask, result: Any?)-> Void in
            success(result!)
        }
        let failureCallBack = { (task: URLSessionDataTask?, error: Error)-> Void in
            failed(error)
        }
        if methodType == RequestType.GET {
            get(urlString, parameters: params, success: successCallBack, failure: failureCallBack)
        }else{
            post(urlString, parameters: params, success: successCallBack, failure: failureCallBack)
        }
    }
}

